/**
 * 武汉天瑞兴科技有限公司
 */
package com.adds.lvds.service.impl.organize;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.adds.lvds.core.utils.CommUtils;
import com.adds.lvds.dao.impl.organize.OrganizeInfoMapper;
import com.adds.lvds.model.organize.SysOrgInfo;
import com.adds.lvds.model.organize.SysOrgUser;
import com.adds.lvds.model.organize.SysUserInfo;

/**
 * 项目名称 ：lvds-web-service
 * 
 * @author zhangjian
 * @time 2016-8-16下午4:25:34
 * @desc 组织结构业务层
 */
@Service("organizeInfoService")
@Transactional(rollbackFor = Exception.class)
public class OrganizeInfoService {
	@Autowired
	private OrganizeInfoMapper organizeInfoMapper;

	/**
	 * 根据条件检索获取部门数据
	 * 
	 * @param map
	 * @return
	 * @throws Exception
	 */
	public List<SysOrgInfo> findSysOrgInfoByCondition(Map<String, Object> map) {
		List<SysOrgInfo> list = null;
		try {
			list = organizeInfoMapper.findSysOrgInfoByCondition(map);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	/**
	 * 查询当前机构及包含的成员和下级机构
	 * 
	 * @param String
	 * @return
	 */
	public SysOrgInfo querySysOrgInfoOnlyByOrgId(String org_id) {
		SysOrgInfo sysOrgInfo = null;
		try {
			sysOrgInfo = organizeInfoMapper.querySysOrgInfoOnlyByOrgId(org_id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return sysOrgInfo;
	}

	/**
	 * 根据条件获取组织树对象数据
	 * 
	 * @param map
	 * @return
	 */
	public SysOrgInfo findSysOrgInfoByMap(Map<String, Object> map) {
		SysOrgInfo sysOrgInfo = null;
		try {
			sysOrgInfo = organizeInfoMapper.findSysOrgInfoByMap(map);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return sysOrgInfo;
	}

	/**
	 * 查询当前机构及下级机构
	 * 
	 * @param String
	 * @return
	 */
	public SysOrgInfo findCurrentOrgAndChildOrgs(String org_id) {
		SysOrgInfo sysOrgInfo = new SysOrgInfo();
		try {
			sysOrgInfo = organizeInfoMapper.findCurrentOrgAndChildOrgs(org_id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return sysOrgInfo;
	}

	/**
	 * 查询当前机构及包含的成员和下级机构
	 * 
	 * @param String
	 * @return
	 */
	public SysOrgInfo findSysOrgAndUserByOrgId(String org_id) {
		SysOrgInfo sysOrgInfo = new SysOrgInfo();
		try {
			sysOrgInfo = organizeInfoMapper.findSysOrgAndUserByOrgId(org_id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return sysOrgInfo;
	}

	/**
	 * 查询当前机构及包含的成员
	 * 
	 * @param String
	 * @return
	 */
	public SysOrgInfo findOrgUserByOrgId(String org_id) {
		SysOrgInfo sysOrgInfo = new SysOrgInfo();
		try {
			sysOrgInfo = organizeInfoMapper.findOrgUserByOrgId(org_id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return sysOrgInfo;
	}

	/**
	 * 查询下级机构 通过parent_org_id
	 * 
	 * @param String
	 * @return
	 */
	public List<SysOrgInfo> findChildOrgByParentOrgId(String parent_org_id) {
		List<SysOrgInfo> list = new ArrayList<SysOrgInfo>();
		try {
			list = organizeInfoMapper.findChildOrgByParentOrgId(parent_org_id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	/**
	 * 根据org_id查询机构下人员数量
	 * 
	 * @param java
	 *            .lang.Integer
	 * @return
	 */
	public int getUserNumByOrgId(int org_id) {
		int num = 0;
		try {
			num = organizeInfoMapper.getUserNumByOrgId(org_id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return num;
	}

	/**
	 * 根据条件获取组织下的所有数据
	 * 
	 * @param map
	 * @return
	 */
	public List<SysOrgInfo> getSysOrgInfoByMap(Map<String, Object> map) {
		List<SysOrgInfo> list = null;
		try {
			list = organizeInfoMapper.getSysOrgInfoByMap(map);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	/**
	 * 录入组织数据
	 * 
	 * @param sysOrgInfo
	 */
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public SysOrgInfo insertSysOrgInfo(SysOrgInfo sysOrgInfo) {
		String result = CommUtils.SUCC;
		try {
			organizeInfoMapper.insertSysOrgInfo(sysOrgInfo);
			sysOrgInfo.setReserved1(result);
		} catch (Exception e) {
			throw new RuntimeException();
		}
		return sysOrgInfo;

	}

	/**
	 * 编辑组织数据
	 * 
	 * @param sysOrgInfo
	 */
	public String updateSysOrgInfo(SysOrgInfo sysOrgInfo) {
		String result = CommUtils.SUCC;
		try {
			organizeInfoMapper.updateSysOrgInfo(sysOrgInfo);
		} catch (Exception e) {
			result = CommUtils.ERROR;
			e.printStackTrace();
		}
		return result;

	}

	/**
	 * 通过parent_org_id获取与该数值相同的parent_org_id的所有信息
	 * 
	 * @param parent_id
	 * @return
	 */
	public List<SysOrgInfo> findSysOrgByParentId(String parent_id) {
		List<SysOrgInfo> list = null;
		try {
			list = organizeInfoMapper.findSysOrgByParentId(parent_id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	/**
	 * 通过org_id获取emp_id
	 * 
	 * @param org_id
	 * @return
	 */
	public List<SysUserInfo> findEmpIdByOrgId(String org_id) {
		List<SysUserInfo> list = null;
		try {
			list = organizeInfoMapper.findEmpIdByOrgId(org_id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	/**
	 * 添加信息
	 * 
	 * @param sysOrgUser
	 */
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void insertSysOrgUser(SysOrgUser sysOrgUser) {
		try {
			this.organizeInfoMapper.insertSysOrgUser(sysOrgUser);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	/**
	 * 删除组织数据
	 * 
	 * @param map
	 */
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public String deleteSysOrgInfo(String org_id) {
		String result = CommUtils.SUCC;
		try {

			organizeInfoMapper.deleteSysOrgInfo(org_id);
		} catch (Exception e) {
			result = CommUtils.ERROR;
			throw new RuntimeException();
		}
		return result;

	}

	/**
	 * 删除该父节点id
	 * 
	 * @param parentId
	 */
	@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
	public void deleteByParentId(String parentId) {
		try {
			organizeInfoMapper.deleteByParentId(parentId);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	/**
	 * 根据条件检索 SysOrgUser 表的数据
	 * 
	 * @param map
	 * @return
	 */
	public List<SysOrgUser> findSysOrgUserByMap(Map<String, Object> map) {
		List<SysOrgUser> list = null;
		try {
			list = organizeInfoMapper.findSysOrgUserByMap(map);
		} catch (Exception e) {
			// TODO: handle exception
		}
		return list;
	}
	/**
	 * 根据条件查询返回树形结构列表数据
	 * @param map
	 * @return
	 * @throws Exception
	 */
	public List<SysOrgInfo> queryTreeOrgByMap(Map<String,Object> map){
		List<SysOrgInfo> list = new ArrayList<SysOrgInfo>();
		try {
			list = organizeInfoMapper.queryTreeOrgByMap(map);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}
}
